import modelExtend from 'dva-model-extend'
import { lwjPageModel } from 'utils/model'
import { query, listRoles } from './services/financialReceipt'
import { routerRedux } from 'dva/router';


export default modelExtend(lwjPageModel, {
  namespace: 'receipt',
  state: {
    currentItem: {},
    pageOptions: {
      sort: 'createTime',
      order: 'DESC'
    },
    isExpand: false,
    isCashier: false,
    queryParam: {},
    currentType:"SALES"
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/receipt') {
          dispatch({ type: 'setCurrentTab', payload: "SALES" })
          dispatch({ type: 'query', payload: {} })
          dispatch({ type: 'isCashier', payload: {} })
        }
      })
    }
  },
  effects: {
    * query({
      payload
    }, { select, call, put }) {

      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      });

      const pageOptions = yield select(({ receipt }) => receipt.pageOptions);
      const queryParam = yield select(({ receipt }) => receipt.queryParam);
      const currentType = yield select(({ receipt }) => receipt.currentType);

      const queryPayload = {
        ...pageOptions,
        queryParam: {
          ...queryParam,
          accountType:currentType
        },
        // ...payload
      };
      const data = yield call(query, queryPayload);
      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },
    * showDetail({
      payload
    }, { select, call, put }) {
      yield put(routerRedux.push(`/receipt/detail/${payload.recordId}`));
    },
    * showAdd({
      payload
    }, { select, call, put }) {
      yield put(routerRedux.push(`/receipt/add`));
    },
    * showAudit({
      payload
    }, { select, call, put }) {
      yield put(routerRedux.push(`/receipt/audit/${payload.recordId}`));
    },
    * showList({
      payload
    }, { select, call, put }) {
      yield put(routerRedux.push(`/receipt`));
    },
    * showSuccess({
      payload
    }, { select, call, put }) {
      yield put(routerRedux.push(`/success`));
    },
    * isCashier({
      payload
    }, { select, call, put }) {
      const data = yield call(listRoles, payload)
      if (data.code === 0) {
        const result = data.result;
        const isCashierBool = result.some((item, index, array) => {
          return item.name === "ROLE_TENANT_CASHIER"
        })
        if (isCashierBool) {
          yield put({
            type: 'setCashier',
            payload: true
          })
        } else {
          yield put({
            type: 'setCashier',
            payload: false
          })
        }
      }
    },
  },
  reducers: {
    showModalDetail(state, { payload }) {
    },
    expandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: true
      }
    },
    unExpandFilter(state, { payload }) {
      return {
        ...state,
        isExpand: false
      }
    },
    setCashier(state, { payload }) {
      return {
        ...state,
        isCashier: payload
      }
    },
    setCurrentTab(state, { payload }) {
      return {
        ...state,
        currentType: payload
      }
    },
  }
})
